package com.own.business.exam.entity.dto;


import com.own.business.exam.common.exception.ExamException;
import com.own.business.exam.entity.bo.ExamRuleBo;
import com.own.component.common.base.entity.BaseDto;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ExamRuleDto
 *
 * @author chenxueli
 * @date 2021/05/18 14:43:47
 */
@Schema(title = "考试规则表" + ConstantBeanSuffix.DTO)
@Getter
@Setter
@ToString
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class ExamRuleDto extends BaseDto<ExamRuleBo> {

    @Schema(title = "考试id")
    @NotBlank(message = "请选择考试信息")
    private Long examId;

    @Schema(title = "规则名称")
    @NotBlank(message = "请输入规则名称")
    @Length(max = 50, message = "规则名称最大为50个字符")
    private String name;

    @Schema(title = "规则类型 1=指定试卷（所有使用这个规则的考试都会使用指定的试卷进行答题，指定的试卷可以是多份） 2=随机试卷（试卷内容根据规则全随机） 3=弱项试卷（会根据个人的错题内容进行按筛选符合的试卷）")
    @NotNull(message = "请选择规则类型")
    private Integer type;

    @Schema(title = "是否指定账号 1=指定 0=不指定")
    @NotNull(message = "请选择是否指定账号")
    private Integer isAssignAccount;

    @Schema(title = "是否指定人数 1=指定 0=不指定")
    @NotNull(message = "请选择是否指定人数")
    private Integer isAssignNumber;

    @Schema(title = "考试最多容纳的人数 0表示不做限制")
    private Integer assignNumber;

    @Schema(title = "试卷生成的具体json", hidden = true)
    private Integer content;

    @Schema(title = "题目数量")
    @NotNull(message = "请填写题目数量")
    @Range(max = 300, min = 1, message = "题目数量在1~300题范围内容")
    private Integer number;

    @Schema(title = "指定的账号列表 isAssignAccount = 1时才会使用")
    private List<String> accountList;

    @Schema(title = "题目数量信息")
    private List<ExamRuleQuestionCountDto> countDtoList;

    /**
     * 验证数据
     */
    @Override
    public void check() {
        super.check();
        if (countDtoList.isEmpty() || number <= 0) {
            throw new ExamException("题目信息错误，请选择规则题量信息");
        }
        if (isAssignAccount == 1 && accountList.isEmpty()) {
            throw new ExamException("当前规则为指定账号类型，检测到允许的账号列表为空，请添加账号信息");
        }
    }

    /**
     * 初始化
     */
    @Override
    protected void init() {
        super.init();
        if (accountList == null) {
            accountList = new ArrayList<>();
        }
        if (countDtoList == null) {
            countDtoList = new ArrayList<>();
        }
    }

    /**
     * 转业务层对象
     *
     * @param entity 原业务层对象
     * @return 业务层对象
     */
    @Override
    public ExamRuleBo toBo(ExamRuleBo entity) {
        if (entity == null) {
            entity = new ExamRuleBo();
        }
        entity.setExamId(examId);
        entity.setName(name);
        entity.setType(type);
        entity.setIsAssignAccount(isAssignAccount);
        entity.setIsAssignNumber(isAssignNumber);
        entity.setAssignNumber(assignNumber);
        entity.setContent(content);
        entity.setNumber(number);
        entity.setAccountList(accountList);
        entity.setCountList(countDtoList.stream().map(ExamRuleQuestionCountDto::toBo).collect(Collectors.toList()));
        return super.toBo(entity);
    }

}
